#version 110


varying vec2 TexCoord;


uniform vec4 LightPosition;
uniform vec3 LightIntensity;

varying vec3 LightDir;

varying vec3 ViewDir;

uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;


void main()
{
   vec3 norm = normalize(NormalMatrix * gl_Normal);
   vec3 tang;
   vec3 binormal;

   vec3 c1 = cross(gl_Normal, vec3(0.0, 0.0, 1.0)); 
   vec3 c2 = cross(gl_Normal, vec3(0.0, 1.0, 0.0)); 
   
   if(length(c1)>length(c2))
   {
      tang = c1;   
   }
   else
   {
      tang = c2;   
   }
   
   tang = normalize(tang);
   
   binormal = cross(gl_Normal, tang); 
   binormal = normalize(binormal);


mat3 toObjectLocal = mat3(tang.x, binormal.x, norm.x,tang.y, binormal.y, norm.y,tang.z, binormal.z, norm.z ) ;
// Get the position in eye coordinates
vec3 position =   vec3(ModelMatrix * gl_Vertex);
LightDir = normalize( toObjectLocal * LightPosition.xyz - position) ;

ViewDir = toObjectLocal * normalize(-position);


// Pass along the texture coordinate
	TexCoord = gl_MultiTexCoord0.xy;

	gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * gl_Vertex;

}
